<?php
class UserAccountService
{

    /*
    * 发起提现请求
    *
    */
    public static function withdrawRequest($user,$amount,$bank_name,$open_bank,$bankcard_no,$real_name)
    {
        //事务开始
        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();

        try {

            //调整用户的金额情况

            $user_update = UserAccountService::withdraw($user,$amount);

            //增加提现申请记录

            UserAccountService::addWithdrawRequest($user,$amount,$bank_name,$open_bank,$bankcard_no,$real_name);

            //增加现金流水
            UserAccountService::addBankCashLog($user_update,$amount,MsgConst::BANKCASH_LOG_TYPE_WITHDRAWING_SUBMITING,"");





            $transaction->commit();//事务结束
        } catch (Exception $e) {
            $transaction->rollback();//回滚函数

            throw new Exception($e->getMessage());

        }


    }

    /*
     * 工人提现申请修改
     */
    public static function withdraw ($user,$amount)
    {
        $user->balance_withdrawing +=$amount;
        $user->balacan_cash-=$amount;

        Util::updateObjData($user);

        Util::saveObjData($user);

        return $user;


    }

    /*
    * 增加提现记录
    *
    */
    public static function addWithdrawRequest($user,$amount,$bank_name,$open_bank,$bankcard_no,$real_name)
    {
        $withdrawrequest = new UcWithdrawRequest();

        $withdrawrequest->amount = $amount;
        $withdrawrequest->real_name = $real_name;
        $withdrawrequest->bank_name = $bank_name;
        $withdrawrequest->backcard_no = $bankcard_no;
        $withdrawrequest->open_bank = $open_bank;
        $withdrawrequest->user_id = $user->user_id;
        $withdrawrequest->status = MsgConst::WITHDRAW_TYPE_SUBMITING;
        $withdrawrequest->mobile = $user->mobile;
        $withdrawrequest->fee = MsgConst::WITHDRAW_FEE;

        Util::initObjData($withdrawrequest);
        Util::saveObjData($withdrawrequest);

        UserAccountService::addWithdrawRequestLog($withdrawrequest);

    }


    /*
    * 增加提现记录log
    *
    */
    public static function addWithdrawRequestLog($withdrawrequest)
    {
        $log = new UcWithdrawRequestLog();

        $log->user_id = $withdrawrequest->user_id;
        $log->bank_name = $withdrawrequest->bank_name;
        $log->backcard_no = $withdrawrequest->backcard_no;
        $log->open_bank = $withdrawrequest->open_bank;
        $log->status = $withdrawrequest->status;
        $log->request_id = $withdrawrequest->request_id;
        $log->real_name = $withdrawrequest->real_name;
        $log->amount = $withdrawrequest->amount;
        $log->mobile = $withdrawrequest->mobile;
        $log->reason = $withdrawrequest->reason;
        $log->fee =$withdrawrequest->fee;

        Util::initObjData($log);
        Util::saveObjData($log);


    }

    /*
    * 插入现金流水log
    *
    */
    public static function addBankCashLog($user,$amount,$type,$remark)
    {
        $bankcashlog = new UcBankcash();

        $bankcashlog->user_id = $user->user_id;
        $bankcashlog->amount = $amount;
        $bankcashlog->type = $type;
        $bankcashlog->mobile = $user->mobile;

        $bankcashlog->balance = $user->balance;
        $bankcashlog->balance_withdrawing = $user->balance_withdrawing;
        $bankcashlog->balacan_cash = $user->balacan_cash;

        $bankcashlog->income_platform = $user->income_platform;
        $bankcashlog->income_purse = $user->income_purse;
        $bankcashlog->income_order = $user->income_order;

        $bankcashlog->fee = MsgConst::WITHDRAW_FEE_USER;
        $bankcashlog->remark = $remark;

        Util::initObjData($bankcashlog);

        Util::saveObjData($bankcashlog);

    }

    /*
    * 拒绝提现
    *
    */
    public static function denyWithdrawRequest($user,$request,$reason)
    {

        //事务开始
        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();

        try {

            //调整工人的金额情况

            $user_update = UserAccountService::withdrawRollback($user,$request->amount);

            //修改请求状态
            $request->status = MsgConst::WITHDRAW_TYPE_FAILED;
            $request->reason = $reason;
            Util::updateObjData($request);
            Util::saveObjData($request);
            UserAccountService::addWithdrawRequestLog($request);


            //增加现金流水
            UserAccountService::addBankCashLog($user_update,$request->amount,MsgConst::BANKCASH_LOG_TYPE_WITHDRAWING_FAILED,$reason);





            $transaction->commit();//事务结束
        } catch (Exception $e) {
            $transaction->rollback();//回滚函数

            throw new Exception($e->getMessage());

        }



    }

    /*
     * 用户提现申请修改
     */
    public static function withdrawRollback ($user,$amount)
    {
        $user->balance_withdrawing -=$amount;
        $user->balacan_cash+=$amount;

        Util::updateObjData($user);

        Util::saveObjData($user);

        return $user;


    }


    /*
    * 同意提现
    *
    */
    public static function acceptWithdrawRequest($user,$request,$reason)
    {

        //事务开始
        $connection = Yii::app()->db;

        $transaction = $connection->beginTransaction();

        try {

            //调整工人的金额情况

            $user_update = UserAccountService::withdrawProcess($user,$request->amount);

            //修改请求状态
            $request->status = MsgConst:: WITHDRAW_TYPE_ACCEPTED;
            $request->reason = $reason;
            Util::updateObjData($request);
            Util::saveObjData($request);
            UserAccountService::addWithdrawRequestLog($request);


            //增加现金流水
            UserAccountService::addBankCashLog($user_update,$request->amount,MsgConst::BANKCASH_LOG_TYPE_WITHDRAWING_ACCEPTED,$reason);





            $transaction->commit();//事务结束
        } catch (Exception $e) {
            $transaction->rollback();//回滚函数

            throw new Exception($e->getMessage());

        }



    }

    /*
     * 用户提现申请处理
     */
    public static function withdrawProcess ($user,$amount)
    {
        $user->balance_withdrawing -=$amount;
        $user->balance-=$amount;

        Util::updateObjData($user);

        Util::saveObjData($user);

        return $user;


    }













}


?>